<?php
	session_start();
    include("convert_bugID.php");
	//include("../VIEW/frmAddBug.php");
	/*
	echo $_POST["project"]."<br>";
	echo $_POST["device"]."<br>";
	echo $_POST["severity"]."<br>";
	echo $_POST["category"]."<br>";
	echo $_POST["techrepro"]."<br>";
	echo $_POST["status"]."<br>";
	echo $_POST["txtBugTitle"]."<br>";
	echo $_POST["txtBugDescription"]."<br>";
	*/
	if (isset($_POST["project"]) && isset($_POST["device"]) 
				&& isset($_POST["severity"]) && isset($_POST["category"]) 
				&& isset($_POST["techrepro"]) && isset($_POST["status"]) 
				&& isset($_POST["txtBugTitle"]) && isset($_POST["txtBugDescription"])
				&& $_POST["txtBugTitle"] != "" && $_POST["txtBugDescription"] != "" )
	{
		$ret = 0;
		$user_id = $_SESSION["user_id"];
		//$date = date("Y-m-d")." 7:0:0";
        $date = date("Y-m-d H:i:s");
		$project = $_POST["project"];
		$device = $_POST["device"];
		$severity = $_POST["severity"];
		$category = $_POST["category"];
		$techrepro = $_POST["techrepro"];
		$status = $_POST["status"];
		$txtBugTitle = $_POST["txtBugTitle"];
		$txtBugDescription = $_POST["txtBugDescription"];
		include("config.php");
       //$title = urldecode($txtBugTitle);
       //$disc = urldecode($txtBugDescription);
       
        
		$sql_insert_bug = "INSERT INTO bug (bug_id, title,description,severity_id,category_id,techrepro_id,device_id,status_id,project_id) VALUES (null,'".convert_special_string($txtBugTitle)."','".convert_special_string($txtBugDescription)."',$severity,$category,$techrepro,$device,$status,$project)";
		
       // echo $sql_insert_bug;
		if(mysql_query($sql_insert_bug))
		{
			$sql_get_max_bug_num = "select max(bug_id) from bug";
			if(mysql_query($sql_get_max_bug_num))
			{
				$row_get_max_num = mysql_fetch_row(mysql_query($sql_get_max_bug_num));
				$max_num_bug = $row_get_max_num[0];
				$sql_insert_bug_tester = "INSERT INTO bug_tester (bug_tester_id,bug_id,tester_id,status_id,date_post) values (null,$max_num_bug,$user_id,$status,UNIX_TIMESTAMP ('$date'))";
				if(mysql_query($sql_insert_bug_tester))
				{
					
				}
				else
				{
					echo "<br/>Error insert bug_device: " . mysql_error();
					$ret = 1;
				}
			}
			else
			{
				echo "<br/>Error get max num bug: " . mysql_error();
				$ret = 1;
			}
		}
		else
		{
			echo "<br/>Error Insert bug: " . mysql_error();
			$ret = 1;
		}	
		if ($ret == 0)
		{
            $_SESSION['current_sort_field'] = "bug_id";
            $_SESSION['current_sort_by'] = "desc";
			echo "<script>window.location.href='../VIEW/frmMain.php'</script>";
			//echo "success";
		}
	}
	else
	{
		echo "<script>alert('Please fill full data');window.location.href='../VIEW/frmAddBug.php'</script>";
	}
?>
